//
//  ComCopyChildViewController.m
//  WeCommunity
//
//  Created by mac on 2017/8/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ComCopyChildViewController.h"
#import "CommunityLinkTableViewCell.h"
#import "CommunityLinkModel.h"
#import "CWebViewController.h"
#import "CommunityLinkServer.h"


@interface ComCopyChildViewController ()<CommunityLinkCellDelegate>

@end

@implementation ComCopyChildViewController

-(void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)addNotification
{
    //nothing
}

#pragma mark -  取最后一条动态数据
-(CommunityLinkModel *)getLastDynamicObject:(NSArray *)array;
{
    for (NSInteger i = array.count - 1; i >= 0; i --) {
        id model = array[i];
        if ([model isKindOfClass:[CommunityLinkModel class]]) {
            return model;
        }
    }
    
    CommunityLinkModel * model = [CommunityLinkModel new];
    return model;
}

#pragma mark - 加载数据
-(void)loadingData
{
    NSDate * loadDate = nil;
    if (self.loadDataType == LoadDataType_LoadData) {
        loadDate = [self getLastDynamicObject:self.dataSource].createDate;
    }
    [[CommunityLinkServer shared] loadCommunityLinkDataWithType:self.dataType
                                               loadDate:loadDate
                                              loadCount:20
                                               complete:^(BOOL success, NSString *error, NSArray *dataArray) {
                                                   if (success) {
                                                       if (self.loadDataType == LoadDataType_ReLoadData) {
                                                           [self.dataSource removeAllObjects];
                                                       }
                                                       [self.dataSource addObjectsFromArray:dataArray];
                                                       if (dataArray.count == 0) {
                                                           [self.footer setTitle:KHasLoadedAllTitle forState:MJRefreshStateIdle];
                                                           [self.footer setTitle:KHasLoadedAllTitle forState:MJRefreshStateRefreshing];
                                                       }else{
                                                           [self.footer setTitle:KDragOrClickToDragTitle forState:MJRefreshStateIdle];
                                                           [self.footer setTitle:KLoaddingMoreTitle forState:MJRefreshStateRefreshing];
                                                       }
                                                   }else{
                                                       [UITool showHudWithText:error withView:self.view];
                                                   }
                                                   [self endRefreshAnimation:success];
                                               }];
}

#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id  model= self.dataSource[indexPath.row];
    if([model isKindOfClass:[CommunityLinkModel class]]){
        return 96.0;
    }else if ([model isKindOfClass:[AdMobModel class]]){
        if (IS_IPAD) {
            return  KScreen_Width * 0.8;
        }else{
            return  KScreen_Width;
        }
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id  model = self.dataSource[indexPath.row];
    if([model isKindOfClass:[CommunityLinkModel class]]){
        static NSString * dynamicCellID = @"dynamicCopyCell";
        CommunityLinkTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:dynamicCellID];
        if (!cell) {
            cell = [[CommunityLinkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:dynamicCellID];
            cell.delegate = self;
        }
        
        [cell creatCellWithModel:model];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if ([model isKindOfClass:[AdMobModel class]]){
        static NSString * ADCellID = @"AdMobCopyCell";
        DynamicAdMobTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ADCellID];
        if (!cell) {
            cell =[[DynamicAdMobTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ADCellID];
        }
        [cell creatCellWithModel:self.adMobModel root:self];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        return nil;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (![self canProcessNext]) {
        return;
    }
    
    id  model = self.dataSource[indexPath.row];
    if([model isKindOfClass:[CommunityLinkModel class]]){
        CWebViewController * webVC = [[CWebViewController alloc] init];
        webVC.linkUrl = ((CommunityLinkModel *)model).linkUrl;
        [self.navigationController pushViewController:webVC animated:YES];
    }
}
#pragma mark - tableviewCell delegate 点赞
-(void)communityLinkCellClickLikeSender:(UIView *)sender
                              clickCell:(CommunityLinkTableViewCell *)cell
{
    if (![self canProcessNext]) {
        return;
    }
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
    CommunityLinkModel * model = self.dataSource[cellIndexPath.row];
    
    [[CommunityLinkServer shared] likeOrUnLikeDataWithCommunity:model complete:^(BOOL success, NSString *error, id resultModel) {
        if (success) {
            [cell changeLikeNumWithDynamic:resultModel];
        }else{
            [UITool showHudWithText:error withView:self.view];
        }
    }];
}



@end
